package com.fwc.churchtracker.domo;

import com.fwc.fw.annotation.Form;
import com.fwc.fw.annotation.FormField;
import com.fwc.fw.domo.AbstractPersistableDomo;

import javax.jdo.annotations.Discriminator;
import javax.jdo.annotations.DiscriminatorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.Inheritance;
import javax.jdo.annotations.InheritanceStrategy;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@Form(label="Church Member")
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
@Inheritance(strategy=InheritanceStrategy.NEW_TABLE)
@Discriminator(strategy=DiscriminatorStrategy.CLASS_NAME)
public class ChurchMember extends AbstractPersistableDomo {

    @Persistent(defaultFetchGroup="true")
    private Key churchKey;
    
    @NotPersistent
    @FormField(label="Church", controller="/church", renderer="select", order=30)
    private Church church;

    @Persistent(defaultFetchGroup="true")
    private Key memberKey;
    
    @NotPersistent
    @FormField(label="Member", controller="/member", renderer="autocomplete", order=40)
    private Member member;

	public Key getChurchKey() {
		return churchKey;
	}

	public void setChurchKey(Key leagueKey) {
		this.churchKey = leagueKey;
	}

	public Church getChurch() {
		return church;
	}

	public void setLeague(Church church) {
		this.church = church;
		if(church==null) {
			setChurchKey(null);
		} else {
			setChurchKey(KeyFactory.stringToKey(church.getKey()));
		}
	}

	public Key getMemberKey() {
		return memberKey;
	}

	public void setMemberKey(Key memberKey) {
		this.memberKey = memberKey;
	}

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
		if(member==null) {
			setMemberKey(null);
		} else {
			setMemberKey(KeyFactory.stringToKey(member.getKey()));
		}
	}
    
   	public String toString() {
   		StringBuffer sb = new StringBuffer();
   		if(getMember()!=null) {
   			sb.append(getMember());
   		}
   		if(getMember()!=null) {
   			sb.append(" : "+getMember());
   		}
   		return sb.toString();
   	}
}